package com.shxy.api.controller;

import com.shxy.api.bean.news.CatalogBean;
import com.shxy.api.bean.user.RoleBean;
import com.shxy.api.bean.user.UserInfo;
import com.shxy.api.common.ApiResult;
import com.shxy.api.controller.ui.NewsAdd;
import com.shxy.api.dao.news.CatalogDao;
import com.shxy.api.dao.user.RoleDao;
import com.shxy.api.dao.user.UserDao;
import com.shxy.api.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired private UserDao userDao;
    @Autowired private RoleDao roleDao;
    @Autowired private CatalogDao catalogDao;

    @PostMapping("/news/add")
    public ApiResult newsAdd(@Valid @RequestBody NewsAdd news) {
        return ApiResult.success();
    }

    @GetMapping("/user/role/init")
    public ApiResult initUserRole() {
        // 1、获取全部用户
        List<UserInfo> users = userDao.findAll();

        // 2、获取游客角色
        RoleBean role = roleDao.findByName("guest");

        users.stream().forEach(userInfo -> {
            userInfo.setRole(role);
            userDao.save(userInfo);
        });
        return ApiResult.create(users);
    }

    @GetMapping("/catalog/parent/null")
    public ApiResult parentNullCatalog() {
        List<CatalogBean> nodes = catalogDao.findAllByParentIsNull();
        return ApiResult.create(nodes);
    }
}
